package com.zzs.server.controller;

import com.zzs.common.core.response.PageResult;
import com.zzs.common.core.response.Result;
import com.zzs.server.pojo.bo.problem.SaveProblemBO;
import com.zzs.server.pojo.bo.problem.UpdateProblemBO;
import com.zzs.server.pojo.dto.problem.SaveProblemDTO;
import com.zzs.server.pojo.dto.problem.UpdateProblemDTO;
import com.zzs.server.pojo.query.ProblemQuery;
import com.zzs.server.pojo.vo.ProblemDetailVO;
import com.zzs.server.pojo.vo.ProblemVO;
import com.zzs.server.service.ProblemService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 题目管理控制器
 * 处理前端请求，提供题目的增、改、查接口
 * @author zzs
 * @date 2025/03/17
 */
@RestController
@RequestMapping("/server/problem")
public class ProblemController {
    @Autowired
    private ProblemService problemService;

    /**
     * 新增题目
     *
     * @param saveProblemDTO 前端传输对象，包含题目的基本信息
     * @return Result<String> 返回成功后的题目ID
     */
    @PostMapping
    public Result<String> saveProblem(@Valid @RequestBody SaveProblemDTO saveProblemDTO) {
        LocalDateTime now = LocalDateTime.now();
        SaveProblemBO saveProblemBO = SaveProblemBO.builder()
                .title(saveProblemDTO.getTitle().trim())
                .content(saveProblemDTO.getContent().trim())
                .difficulty(saveProblemDTO.getDifficulty())
                .createTime(now)
                .updateTime(now)
                .build();
        long problemId = problemService.saveProblem(saveProblemBO);
        return Result.success(String.valueOf(problemId));
    }

    /**
     * 更新题目
     *
     * @param updateProblemDTO 前端传输对象，包含要更新的题目信息
     * @return Result<Void> 返回操作结果
     */
    @PutMapping
    public Result<Void> updateProblem(@Valid @RequestBody UpdateProblemDTO updateProblemDTO) {
        LocalDateTime now = LocalDateTime.now();
        UpdateProblemBO updateProblemBO = UpdateProblemBO.builder()
                .id(updateProblemDTO.getProblemId())
                .title(updateProblemDTO.getTitle().trim())
                .content(updateProblemDTO.getContent().trim())
                .difficulty(updateProblemDTO.getDifficulty())
                .updateTime(now)
                .build();
        problemService.updateProblem(updateProblemBO);
        return Result.success();
    }

    /**
     * 根据 ID 获取题目详情
     *
     * @param problemId 题目ID（String 类型，需转换为 long）
     * @return Result<ProblemDetailVO> 题目详情
     */
    @GetMapping("/{problemId}")
    public Result<ProblemDetailVO> getProblem(@PathVariable("problemId") Long problemId) {
        ProblemDetailVO problemDetailVO = problemService.getProblem(problemId);
        return Result.success(problemDetailVO);
    }

    @DeleteMapping("/{problemId}")
    public Result<Void> removeProblem(@PathVariable("problemId") Long problemId) {
        problemService.removeProblem(problemId);
        return Result.success();
    }

    @GetMapping
    public Result<PageResult<ProblemVO>> listProblems(@Valid ProblemQuery problemQuery) {
        problemQuery.setKeyword(problemQuery.getKeyword().trim());
        PageResult<ProblemVO> pageResult = problemService.listProblems(problemQuery);
        return Result.success(pageResult);
    }

    @PutMapping("/publish/{problemId}")
    public Result<Void> publishProblem(@PathVariable("problemId") Long problemId) {
        problemService.publishProblem(problemId);
        return Result.success();
    }

    @PutMapping("/cancel/{problemId}")
    public Result<Void> cancelProblem(@PathVariable("problemId") Long problemId) {
        problemService.cancelProblem(problemId);
        return Result.success();
    }
}
